package com.example.controller;

import com.example.entity.Orders;
import com.example.service.impl.OrdersServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Controller
@RequestMapping("/admin")
public class OrdersCtrl {
    @Resource
    OrdersServiceImpl ordersService;

    @GetMapping("/getOrderByID")
    public Orders getOrderByID(Integer id) {return this.ordersService.queryById(id);}

    @RequestMapping(value = "/orders",method = {RequestMethod.POST,RequestMethod.GET})
    public String findAllOrders(Model model, Orders orders,
                                @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",required = false, defaultValue = "4") Integer pageSize){
        PageInfo<Orders> ordersListPage=  ordersService.queryAll(orders,pageNum,pageSize);

        model.addAttribute("ordersListPage",ordersListPage);
        return "ordersList";
    }

    @GetMapping("/delOrders/{id}")
    public String delUsers(@PathVariable("id") int id, Model model) {
        ordersService.deleteById(id);

        model.addAttribute("msg", "删除成功");
        return "forward:/admin/orders";
    }

    @GetMapping("/orders/{id}")
    public String getOrdersById(@PathVariable("id") int id, Model model) {
        Orders orders = ordersService.queryById(id);
        model.addAttribute("orders", orders);
        return "ordersEdit";
    }
    @PostMapping("/editOrders")
    public String updateOrders(Orders orders,Model model){

        System.out.println(orders);
        Orders i =ordersService.update(orders);
        model.addAttribute("msg","编辑成功！");
        return "forward:/admin/orders";
    }

    @GetMapping("/gotoAddOrders")
    public String gotoAddorders(){

        return "ordersAdd";
    }

    @PostMapping("/addOrders")
    public String addOrders(Orders orders){

        Orders i= ordersService.insert(orders);
        return "forward:/admin/orders";
    }
}
